/*
 * Copyright 2013 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.ibator.CoreFriend;
import com.fujitsu.nanjing.tieba.ibator.CoreFriendCriteria;
import com.fujitsu.nanjing.tieba.ibator.CoreFriendKey;
import com.fujitsu.nanjing.tieba.model.Friend;

/**
 * FriendService.java
 * 
 * @author Administrator
 */
@Service
@Transactional
public class FriendService extends BaseService<CoreFriend, CoreFriendCriteria> {
	@Resource
	private Friend friend;
	@Deprecated
	@Override
	public CoreFriend findById(String id) {
		return null;
	}

	@Override
	public CoreFriend insert(CoreFriend t) {
		t.setCreatetime(new Date());
		t.setUserid(getUserDetails().getUsername());
		friend.insert(t);
		return t;
	}

	@Override
	public void update(CoreFriend t) {
		friend.updateByPrimaryKeySelective(t);

	}

	@Override
	public void delete(CoreFriend t) {
		CoreFriendKey _key = new CoreFriendKey();
		_key.setFriendid(t.getFriendid());
		_key.setUserid(t.getUserid());
		friend.deleteByPrimaryKey(_key);

	}
	public void delete(String userId, String friendId) {
		CoreFriendKey _key = new CoreFriendKey();
		_key.setFriendid(friendId);
		_key.setUserid(userId);
		friend.deleteByPrimaryKey(_key);
	}
	public List<CoreFriend> findByUseridWithPageNum(String userid, int offset) {
		CoreFriendCriteria example = new CoreFriendCriteria();
		example.setMysqlLength(CommonParamter.默认分页长度);
		if (offset > 0) {
			example.setMysqlOffset((offset - 1) * CommonParamter.默认分页长度);
		}
		example.setOrderByClause(CommonParamter.默认排序条件);
		example.createCriteria().andUseridEqualTo(userid);
		return friend.selectByExample(example);
	}
	public int countByUserId(String userId) {
		CoreFriendCriteria example = new CoreFriendCriteria();
		example.createCriteria().andUseridEqualTo(userId);
		return friend.countByExample(example);
	}
	/**
	 * 根据用户获取<code>friend</code>总页数
	 * */
	public int findPageCountByUserId(String userId) {
		int pageCount = (int) Math.ceil((double) countByUserId(userId)
				/ CommonParamter.默认分页长度);
		if (pageCount == 0) {
			pageCount = 1;
		}
		return pageCount;
	}

	public List<CoreFriend> findByFriendid(String friendid, int offset) {
		CoreFriendCriteria example = new CoreFriendCriteria();
		example.setMysqlLength(CommonParamter.默认分页长度);
		if (offset > 0) {
			example.setMysqlOffset((offset - 1) * CommonParamter.默认分页长度);
		}
		example.setOrderByClause(CommonParamter.默认排序条件);
		example.createCriteria().andFriendidEqualTo(friendid);
		return friend.selectByExample(example);
	}
	public int countByFriendId(String friendId) {
		CoreFriendCriteria example = new CoreFriendCriteria();
		example.createCriteria().andFriendidEqualTo(friendId);
		return friend.countByExample(example);
	}
	/**
	 * 根据friendId获取<code>friend</code>总页数
	 * */
	public int findPageCountByFriendId(String friendId) {
		int pageCount = (int) Math.ceil((double) countByFriendId(friendId)
				/ CommonParamter.默认分页长度);
		if (pageCount == 0) {
			pageCount = 1;
		}
		return pageCount;
	}
	/**
	 * 此方法未登录用户调用不执行
	 **/
	public void deleteFriends(String friendID) {
		UserDetails userDetails = getUserDetails();
		if (userDetails == null) {
			return;
		}
		String userId = userDetails.getUsername();
		if (friendID != null) {
			String[] friendIds = friendID.split(",");
			for (String friendId : friendIds) {
				delete(userId, friendId);
			}
		}

	}

	public CoreFriend findByKey(String userid, String friendid) {
		CoreFriendKey _key = new CoreFriendKey();
		_key.setFriendid(friendid);
		_key.setUserid(userid);
		return friend.selectByPrimaryKey(_key);
	}

	public void insert(String friendID, String remark) {
		CoreFriend coreFriend = new CoreFriend();
		coreFriend.setFriendid(friendID);
		coreFriend.setRemark(remark);
		insert(coreFriend);

	}
}
